#include "noteslist.h"

NoteBooks::NoteBooks(QWidget *parent) : QTreeWidget(parent)
	{
		setColumnCount(2);
		setColumnHidden(1, true);
		setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
		setHeaderLabel(tr("Book Shelf"));
		setMinimumWidth(150);
		setMaximumWidth(250);

		listNoteBooks();
		setSortingEnabled(true);
		sortByColumn(0, Qt::AscendingOrder);
	}

NoteBooks::~NoteBooks(){}

void NoteBooks::listNoteBooks()
	{
		QDir notesDir(QDir::toNativeSeparators(QDir::homePath()+"/.picKel/notes"));
		notesDir.setFilter(QDir::NoDotAndDotDot | QDir::Dirs | QDir::NoSymLinks);
		notesDir.setSorting(QDir::Name);
		QStringList list = notesDir.entryList();
		list.prepend(".");
		for (int i = 0; i < list.size(); i++)
		{
			QTreeWidgetItem *item = new QTreeWidgetItem(this);
			item->setText(0, list.at(i));
			item->setText(1, notesDir.absoluteFilePath(list.at(i)));
			item->setIcon(0, QIcon(":/icons/book2.png"));
			checkForChildren(item);
		}
	}

void NoteBooks::checkForChildren(QTreeWidgetItem *item)
{
	if (item->text(0) != ".")
	{
		QDir children(item->text(1));
		children.setFilter(QDir::NoDotAndDotDot | QDir::Dirs | QDir::NoSymLinks);
		children.setSorting(QDir::Name);
		QStringList list = children.entryList();
		if (list.count() > 0)
		{
			for (int i = 0; i < list.size(); i++)
			{
				QTreeWidgetItem *childItem = new QTreeWidgetItem(item);
				childItem->setText(0, list.at(i));
				childItem->setText(1, children.absoluteFilePath(list.at(i)));
				childItem->setIcon(0, QIcon(":/icons/book2.png"));
				checkForChildren(childItem);
			}
		}
	}
}
